<?php
if (!isset($_GET['op'])) {
    // header('HTTP/1.1 403 ');
    echo '参数错误，未授权的操作';
    return;
}

require_once 'data/data.php';

$op = $_GET['op'];

$title = '新用户注册';

$action = 'action/handle_regiter_action.php';

$email = '';
$username = '';
$gender = 1;
$coursesArr = ['', '', ''];
$birth = '';
$attr = 1;
$describ = '';
$coursesBase = ['html5', 'css', 'php', 'mysql', 'linux'];
$props = [
    ['value' => 1, 'text' => '狂拽青年'],
    ['value' => 2, 'text' => '酷炫青年'],
    ['value' => 3, 'text' => '屌炸青年'],
    ['value' => 4, 'text' => '冲霄青年']
];

if ($op == 2) {
    $action = 'action/handle_edit_user.php';

    $title = '修改用户信息';
    $id = $_GET['id'];

    $sql = 'select id,email,user_name,gender,cuorses,birthday,attr,introducion from users where id=?';
    $user = query($sql, [$id])[0];

    $email = $user['email'];
    $username = $user['user_name'];
    $gender = $user['gender'];

    $courses = explode(',', $user['cuorses']);
    for ($i = 0; $i < count($courses); $i++) {
        $coursesArr[$i] = trim($courses[$i]);
    }

    $birth = $user['birthday'];
    $attr= $user['attr'];
    $describ = $user['introducion'];
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
	<title><?php echo $title ?></title>
</head>
<body>
  <form method="post" action="<?php echo $action ?>">
    <table align="center" border="1" cellpadding="10" cellspacing="0">
    <tr>
    <th align="center" colspan="3">
        <strong><?php echo $title ?></strong>
    </th>
    </tr>
    
    <tr>
    <td align="right">
        <label for="email">邮&nbsp;&nbsp;箱:</label>
    </td>
    <td colspan="2" align="left">
        <input type="text" id="email" name="email" placeholder="邮箱" value="<?= $email ?>" /> <br/>
    </td>
    </tr>

    <?php if($op == 1): ?>
    <tr>
    <td align="right">
        <label for="password">密码:</label>
    </td>
    <td colspan="2" align="left">
        <input type="password" name="password" placeholder="密码"/> <br/>
    </td>
    </tr>
    
    <tr>
    <td align="right">
        <label for="pwd_again">再次输入密码：</label>
    </td>
    <td colspan="2" align="left">
        <input type="password" name="pwd_again" placeholder="再次输入密码"/> <br/>
    </td>
    </tr>
  <?php endif;?>

    <tr>
    <td align="right">
        <label for="username">用户名：</label>
    </td>
    <td colspan="2" align="left">
        <input type="text" id="username" name="username" placeholder="用户名" value="<?= $username?>" />
    </td>
    </tr>

    <tr>
    <td align="right">
        <label for="age">年龄：</label>
    </td>
    <td colspan="2" align="left">
        <input type="text" name="age" placeholder="年龄"/>
    </td>
    </tr>

    <tr>
    <td align="right">
        <label for="radio">性别：</label>
    </td>
    <td>
        <input type="radio" id="male" name="gender" value="1" <?php if($gender == 1): ?> checked="checked" <?php endif; ?>/>
        <label for="male">男</label>
        <input type="radio" id="female" name="gender" value="0" <?php if ($gender == 0): ?> checked="checked" <?php endif; ?> />
        <label for="female">女</label>
    </td>
    </tr>

    <tr>
    <td align="right">
    <label>主修：</label>
    </td>
    <td>
        <?php foreach ($coursesBase as $c): ?>
            <input type="checkbox" id="<?= $c ?>" name="course[]" value="<?= $c ?>"
          <?php foreach ($coursesArr as $course): ?>
            <?php if($course == $c): ?> checked="checked" <?php endif; ?>
          <?php endforeach; ?>/>
          <label for="<?= $c?>"><?= $c?></label>
        <?php endforeach; ?>
    </td>
    </tr>

    <tr>
    <td align="right">
        <label for="birth">出生年月：</label>
    </td>
    <td colspan="2" align="left">
        <input type="date" name="birth" id="birth" value="<?= $birth?>" />
    </td>
    </tr>

    <tr>
    <td align="right">
        <label for="attr">属性：</label>
    </td>
    <td>
    <select id="attr" name="attr">
       <?php foreach($props as $item): ?>
          <option <?php if($item['value'] == $attr): ?> selected="selected" <?php endif;?> value="<?=$item['value'] ?>"><?= $item['text'] ?></option>
       <?php endforeach; ?>
    </select>
    </td>
    </tr>

    <tr>
    <td align="right">
        <label for="describ">自我介绍：</label>
    </td>
    <td colspan="2" align="left">
        <textarea id="describ" name="describ" rows="5" cols="50"><?php echo $describ ?></textarea>
    </td>
    </tr>

    <tr>
    <td colspan="3" align="center"><!-- <input type="submit" value="点我" /> -->
        <input type="submit" value="确定">
    </td>
    </tr>
    </table>
      <?php if(isset($id)): ?>
         <input type="hidden" name="id" value="<?=$id ?>">
      <?php endif; ?>
   </form>

</body>
</html>